package axht.trade.ui;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class DialogActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog);
    }

    /**
     * 显示一般AlertDialog
     * @param view
     */
    public void showAD(View view) {
        new AlertDialog.Builder(this)
                .setTitle("删除数据")//设置标题
                .setMessage("你确定删除数据吗")
                .setPositiveButton("删除", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(DialogActivity.this,"删除数据",Toast.LENGTH_LONG).show();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(DialogActivity.this,"取消删除数据",Toast.LENGTH_LONG).show();
                    }
                })
                .show();
    }

    /**
     * 显示单选列表AlertDialog
     * @param view
     */
    public void showLD(View view) {
        final String[] items = {"红","蓝","绿","灰"};//final的变量在方法执行完后还存在
        new AlertDialog.Builder(this)
                .setTitle("指定背景颜色")
                .setSingleChoiceItems(items, 2, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //提示颜色
                        Toast.makeText(DialogActivity.this,items[which],Toast.LENGTH_LONG).show();
                        //移除dialog
                        dialog.dismiss();
                    }
                })
                .show();
    }

    /**
     * 显示自定义AlertDialog
     * @param v
     */
    public void showCD(View v) {
        //动态加载布局文件，得到对应的View对象
        View view = View.inflate(this,R.layout.dialog_view,null);
        //问题1：view 的真实类型？是布局文件根标签的类型，包含了子View对象
        //问题2：如何得到一个独立view的子view?   view.findViewById(id)
        //  findViewById(id)是在setContentView()中的View中找
        final EditText nameET = view.findViewById(R.id.et_dialog_name);
        final EditText pwdET = view.findViewById(R.id.et_dialog_pwd);

        new AlertDialog.Builder(this)
                .setView(view)
                .setNegativeButton("取消",null)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //读取用户名和密码
                        String username = nameET.getText().toString();
                        String pwd = pwdET.getText().toString();
                        //提示
                        Toast.makeText(DialogActivity.this,username+" : "+pwd,Toast.LENGTH_LONG).show();
                    }
                })
                .show();


    }

    /**
     * 显示圆形进度ProgressDialog
     * @param view
     */
    public void showPD(View view) {
        final ProgressDialog dialog = ProgressDialog.show(this,"数据加载","数据加载中...");
        //模拟做一个长时间的工作
        //长时间的工作不能在主线程做，得启动分线程完成
        new Thread(){
            @Override
            public void run() {
                for(int i=0;i<20;i++){
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                //移除dialog
                dialog.dismiss();//方法在分线程执行，但内部使用Handler实现主线程移除dialog

                //不能在分线程直接更新UI
                //显示Toast
                //Toast.makeText(DialogActivity.this,"加载完成了！！！",Toast.LENGTH_LONG).show();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {//在主线程执行
                        Toast.makeText(DialogActivity.this,"加载完成了！！！",Toast.LENGTH_LONG).show();
                    }
                });
            }
        }.start();
    }

    /**
     * 显示水平进度ProgressDialog
     * @param view
     */
    public void showPD2(View view) {
        //1 创建dialog对象
        final ProgressDialog progressDialog = new ProgressDialog(this);
        //2 设置样式
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        //3 显示
        progressDialog.show();

        //4 启动分线程，加载数据，并显示进度。当加载完成移除dialog
        new Thread(new Runnable() {
            @Override
            public void run() {
                int count=20;
                //设置最大进度
                progressDialog.setMax(count);
                for (int i=0;i<count;i++){
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    progressDialog.setProgress(progressDialog.getProgress()+1);
                }
                //移除dialog
                progressDialog.dismiss();
            }
        }

        ).start();

    }

    public void showDateAD(View view) {
    }

    public void showTimeAD(View view) {
    }
}
